/*
 * June 15, 2009
 *
 * about,splash image courtesy of http://scaryfuckingowls.tumblr.com/
 *
 */
package app;

import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.io.Connector;
import javax.microedition.io.PushRegistry;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import org.netbeans.microedition.util.SimpleCancellableTask;

/**
 * @author thirdy
 */
public class ThesisMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;
    private ThesisServer thesisServer;
    private ThesisClient thesisClient;
    private ThesisRMS thesisSrvrSettings;
    private ThesisRMS thesisClntSettings;
    private boolean[] srvrSettingsFlag = new boolean[ThesisConstants.SETTINGS_FLAG_SIZE];
    private String fileNamePrefix = "ths";
    private boolean saveRecsFlag = false;
    private boolean pushRegFlag = false;
    private boolean[] clntSettingsFlag = new boolean[1];
    private String defaultSrvrNum = ThesisConstants.EMPTY;
    private String clientNum = ThesisConstants.EMPTY;
    private boolean startedByReq = false;
    private MessageConnection SMSServer = null;
    private int automatedGaugeValue = 3;
    private int automatedVidDurIdx = 0;
    private String automatedClientNum = ThesisConstants.EMPTY;
    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private List mainMenu;
    private Form thesisServerLog;
    private List thesisClientMenu;
    private TextBox serverNumber;
    private Alert thesisErrorAlert;
    private Alert thesisInfoAlert;
    private Form serverSettingsForm;
    private ChoiceGroup saveRecFlagCG;
    private TextField fnPrfxTextField;
    private ChoiceGroup pushRegCG;
    private TextField clientNumTF;
    private StringItem stringItem;
    private ChoiceGroup automateVidDurCG;
    private TextField AutomatedClientTF;
    private Gauge automatedGauge;
    private ChoiceGroup notificationsCG;
    private Form clientSettingsForm;
    private ChoiceGroup clntSettingsFlgCG;
    private TextField defSrvrTF;
    private Form aboutForm;
    private StringItem stringItem4;
    private StringItem stringItem3;
    private ImageItem imageItem;
    private StringItem stringItem2;
    private StringItem stringItem1;
    private Command exitCommand;
    private Command Send;
    private Command backCommand;
    private Command cancelCommand;
    private Command saveCommand;
    private Command notifcnsCommand;
    private Command okCommand;
    private Command settingsCommand;
    private Command okCommand1;
    private Image serverIcon;
    private Image splashImg;
    private Image clientIcon;
    private Image notifcIcon;
    private Image exitIcon;
    private Image errorIcon;
    private Image downldIcon;
    private Image infoIcon;
    private SimpleCancellableTask task;
    private Image owlMed;
    private Image owlBig;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The ThesisMIDlet constructor.
     */
    public ThesisMIDlet() {
        try {
            thesisSrvrSettings = new ThesisRMS(ThesisConstants.RMS_SRVR_SETTINGS_NAME);
            thesisClntSettings = new ThesisRMS(ThesisConstants.RMS_CLNT_SETTINGS_NAME);
            loadSrvrSettings();
            loadClntSettings();
        } catch (Throwable ex) {
            showErrorAlert(ex);
        }
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|
    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        // check if started by a request via push reg
        if (pushRegFlag) {
            try {
                SMSServer = (MessageConnection) Connector.open("sms://:" + ThesisConstants.SMSPORT);
                SMSServer.setMessageListener(new MessageListener() {

                    public void notifyIncomingMessage(MessageConnection conn) {
                        startedByReq = true;
                    }
                });
                // wait for the req
                Thread.sleep(1000);
            } catch (Throwable ex) {
                showErrorAlert(ex);
            }
            if (startedByReq) {
                switchDisplayable(null, getThesisServerLog());
                initServer();
                return;
            } else {
                try {
                    SMSServer.close();
                } catch (Throwable ex) {
                    showErrorAlert(ex);
                }
            }
        }
//GEN-LINE:|3-startMIDlet|1|3-postAction
       new AboutScreen(getDisplay(), getMainMenu(), 3000, getOwlBig());
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
        switchDisplayable(null, getMainMenu());//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == aboutForm) {//GEN-BEGIN:|7-commandAction|1|192-preAction
            if (command == backCommand) {//GEN-END:|7-commandAction|1|192-preAction
                // write pre-action user code here
                switchDisplayable(null, getMainMenu());//GEN-LINE:|7-commandAction|2|192-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|134-preAction
        } else if (displayable == clientSettingsForm) {
            if (command == cancelCommand) {//GEN-END:|7-commandAction|3|134-preAction
                // write pre-action user code here
                switchDisplayable(null, getThesisClientMenu());//GEN-LINE:|7-commandAction|4|134-postAction
                // write post-action user code here
            } else if (command == saveCommand) {//GEN-LINE:|7-commandAction|5|133-preAction
                // write pre-action user code here
                // validate form
                if (getClntSettingsFlgCG().isSelected(0) && (getDefSrvrTF().getString() == null
                        || getDefSrvrTF().getString().equals(ThesisConstants.EMPTY))) {
                    showErrorAlert("Enter default server number");
                    return;
                }
                saveClntSettings();
//GEN-LINE:|7-commandAction|6|133-postAction
                // write post-action user code here
                getThesisInfoAlert().setString(ThesisConstants.SETTINGS_SAVED_MSG);
                switchDisplayable(getThesisInfoAlert(), getThesisClientMenu());
            }//GEN-BEGIN:|7-commandAction|7|23-preAction
        } else if (displayable == mainMenu) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|7|23-preAction
                // write pre-action user code here
                mainMenuAction();//GEN-LINE:|7-commandAction|8|23-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|9|59-preAction
        } else if (displayable == serverNumber) {
            if (command == Send) {//GEN-END:|7-commandAction|9|59-preAction
                // write pre-action user code here
                String msg = thesisClientMenu.getString(thesisClientMenu.getSelectedIndex());
                thesisClient.sendSMS(msg, serverNumber.getString());
//GEN-LINE:|7-commandAction|10|59-postAction
                // write post-action user code here
                switchDisplayable(null, getThesisClientMenu());
                getServerNumber().setString(ThesisConstants.EMPTY);
            } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|11|60-preAction
                // write pre-action user code here
                switchDisplayable(null, getThesisClientMenu());//GEN-LINE:|7-commandAction|12|60-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|13|119-preAction
        } else if (displayable == serverSettingsForm) {
            if (command == cancelCommand) {//GEN-END:|7-commandAction|13|119-preAction
                // write pre-action user code here
                switchDisplayable(null, getThesisServerLog());//GEN-LINE:|7-commandAction|14|119-postAction
                // write post-action user code here
            } else if (command == saveCommand) {//GEN-LINE:|7-commandAction|15|122-preAction
                // write pre-action user code here
                new Thread(new Runnable() {

                    public void run() {
                            try {
                    saveSrvrSettings();
                                 getThesisInfoAlert().setString(ThesisConstants.SETTINGS_SAVED_MSG);
                switchDisplayable(getThesisInfoAlert(), getThesisServerLog());
                } catch (Throwable t) {
                    showErrorAlert(t);
                }
                    }
                }).start();

//GEN-LINE:|7-commandAction|16|122-postAction
                // write post-action user code here

            }//GEN-BEGIN:|7-commandAction|17|44-preAction
        } else if (displayable == thesisClientMenu) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|17|44-preAction
                // write pre-action user code here
                thesisClientMenuAction();//GEN-LINE:|7-commandAction|18|44-postAction
                // write post-action user code here
            } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|19|51-preAction
                // write pre-action user code here
                switchDisplayable(null, getMainMenu());//GEN-LINE:|7-commandAction|20|51-postAction
                // write post-action user code here
            } else if (command == okCommand1) {//GEN-LINE:|7-commandAction|21|179-preAction
                // write pre-action user code here
                switchDisplayable(null, getClientSettingsForm());//GEN-LINE:|7-commandAction|22|179-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|23|41-preAction
        } else if (displayable == thesisServerLog) {
            if (command == exitCommand) {//GEN-END:|7-commandAction|23|41-preAction
                // write pre-action user code here
                switchDisplayable(null, getMainMenu());//GEN-LINE:|7-commandAction|24|41-postAction
                // write post-action user code here
                getThesisServerLog().deleteAll();
            } else if (command == okCommand) {//GEN-LINE:|7-commandAction|25|168-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|26|168-postAction
                // write post-action user code here
                toggleAutomatedServer();
            } else if (command == settingsCommand) {//GEN-LINE:|7-commandAction|27|161-preAction
                // write pre-action user code here
                loadSrvrSettings();
                switchDisplayable(null, getServerSettingsForm());//GEN-LINE:|7-commandAction|28|161-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|29|7-postCommandAction
        }//GEN-END:|7-commandAction|29|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|30|
    //</editor-fold>//GEN-END:|7-commandAction|30|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: mainMenu ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of mainMenu component.
     * @return the initialized component instance
     */
    public List getMainMenu() {
        if (mainMenu == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            mainMenu = new List("Main menu", Choice.IMPLICIT);//GEN-BEGIN:|22-getter|1|22-postInit
            mainMenu.append("Start as server", getServerIcon());
            mainMenu.append("Start as client", getClientIcon());
            mainMenu.append("About", getInfoIcon());
            mainMenu.append("Exit", getExitIcon());
            mainMenu.setCommandListener(this);
            mainMenu.setSelectedFlags(new boolean[] { false, false, false, false });//GEN-END:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return mainMenu;
    }
    //</editor-fold>//GEN-END:|22-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: mainMenuAction ">//GEN-BEGIN:|22-action|0|22-preAction
    /**
     * Performs an action assigned to the selected list element in the mainMenu component.
     */
    public void mainMenuAction() {//GEN-END:|22-action|0|22-preAction
        // enter pre-action user code here
        String __selectedString = getMainMenu().getString(getMainMenu().getSelectedIndex());//GEN-BEGIN:|22-action|1|26-preAction
        if (__selectedString != null) {
            if (__selectedString.equals("Start as server")) {//GEN-END:|22-action|1|26-preAction
                // write pre-action user code here
                switchDisplayable(null, getThesisServerLog());//GEN-LINE:|22-action|2|26-postAction
                // write post-action user code here
                initServer();
            } else if (__selectedString.equals("Start as client")) {//GEN-LINE:|22-action|3|27-preAction
                // write pre-action user code here
                switchDisplayable(null, getThesisClientMenu());//GEN-LINE:|22-action|4|27-postAction
                // write post-action user code here
                cleanConnections();
                thesisClient = new ThesisClient(this);
            } else if (__selectedString.equals("About")) {//GEN-LINE:|22-action|5|187-preAction
                // write pre-action user code here
                switchDisplayable(null, getAboutForm());//GEN-LINE:|22-action|6|187-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Exit")) {//GEN-LINE:|22-action|7|28-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|22-action|8|28-postAction
                // write post-action user code here
            }//GEN-BEGIN:|22-action|9|22-postAction
        }//GEN-END:|22-action|9|22-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|22-action|10|
    //</editor-fold>//GEN-END:|22-action|10|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: thesisServerLog ">//GEN-BEGIN:|38-getter|0|38-preInit
    /**
     * Returns an initiliazed instance of thesisServerLog component.
     * @return the initialized component instance
     */
    public Form getThesisServerLog() {
        if (thesisServerLog == null) {//GEN-END:|38-getter|0|38-preInit
            // write pre-init user code here
            thesisServerLog = new Form("Server Log");//GEN-BEGIN:|38-getter|1|38-postInit
            thesisServerLog.addCommand(getExitCommand());
            thesisServerLog.addCommand(getSettingsCommand());
            thesisServerLog.addCommand(getOkCommand());
            thesisServerLog.setCommandListener(this);//GEN-END:|38-getter|1|38-postInit
            // write post-init user code here
        }//GEN-BEGIN:|38-getter|2|
        return thesisServerLog;
    }
    //</editor-fold>//GEN-END:|38-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|40-getter|0|40-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|40-getter|0|40-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 1);//GEN-LINE:|40-getter|1|40-postInit
            // write post-init user code here
        }//GEN-BEGIN:|40-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|40-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: thesisClientMenu ">//GEN-BEGIN:|43-getter|0|43-preInit
    /**
     * Returns an initiliazed instance of thesisClientMenu component.
     * @return the initialized component instance
     */
    public List getThesisClientMenu() {
        if (thesisClientMenu == null) {//GEN-END:|43-getter|0|43-preInit
            // write pre-init user code here
            thesisClientMenu = new List("Client Menu", Choice.IMPLICIT);//GEN-BEGIN:|43-getter|1|43-postInit
            thesisClientMenu.append(ThesisConstants.REC3SEC, getDownldIcon());
            thesisClientMenu.append(ThesisConstants.REC5SEC, getDownldIcon());
            thesisClientMenu.append(ThesisConstants.REC10SEC, getDownldIcon());
            thesisClientMenu.addCommand(getExitCommand());
            thesisClientMenu.addCommand(getOkCommand1());
            thesisClientMenu.setCommandListener(this);
            thesisClientMenu.setSelectedFlags(new boolean[] { false, false, false });//GEN-END:|43-getter|1|43-postInit
            // write post-init user code here
        }//GEN-BEGIN:|43-getter|2|
        return thesisClientMenu;
    }
    //</editor-fold>//GEN-END:|43-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: thesisClientMenuAction ">//GEN-BEGIN:|43-action|0|43-preAction
    /**
     * Performs an action assigned to the selected list element in the thesisClientMenu component.
     */
    public void thesisClientMenuAction() {//GEN-END:|43-action|0|43-preAction
        // enter pre-action user code here
        String __selectedString = getThesisClientMenu().getString(getThesisClientMenu().getSelectedIndex());//GEN-BEGIN:|43-action|1|54-preAction
        if (__selectedString != null) {
            if (__selectedString.equals(ThesisConstants.REC3SEC)) {//GEN-END:|43-action|1|54-preAction
                // write pre-action user code here
                if (clntSettingsFlag[0]) {
                    String msg = thesisClientMenu.getString(thesisClientMenu.getSelectedIndex());
                    thesisClient.sendSMS(msg, defaultSrvrNum);
                    return;
                }
                switchDisplayable(null, getServerNumber());//GEN-LINE:|43-action|2|54-postAction
                // write post-action user code here
            } else if (__selectedString.equals(ThesisConstants.REC5SEC)) {//GEN-LINE:|43-action|3|109-preAction
                // write pre-action user code here
                if (clntSettingsFlag[0]) {
                    String msg = thesisClientMenu.getString(thesisClientMenu.getSelectedIndex());
                    thesisClient.sendSMS(msg, defaultSrvrNum);
                    return;
                }
                switchDisplayable(null, getServerNumber());//GEN-LINE:|43-action|4|109-postAction
                // write post-action user code here
            } else if (__selectedString.equals(ThesisConstants.REC10SEC)) {//GEN-LINE:|43-action|5|110-preAction
                // write pre-action user code here
                if (clntSettingsFlag[0]) {
                    String msg = thesisClientMenu.getString(thesisClientMenu.getSelectedIndex());
                    thesisClient.sendSMS(msg, defaultSrvrNum);
                    return;
                }
                switchDisplayable(null, getServerNumber());//GEN-LINE:|43-action|6|110-postAction
                // write post-action user code here
            }//GEN-BEGIN:|43-action|7|43-postAction
        }//GEN-END:|43-action|7|43-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|43-action|8|
    //</editor-fold>//GEN-END:|43-action|8|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Send ">//GEN-BEGIN:|48-getter|0|48-preInit
    /**
     * Returns an initiliazed instance of Send component.
     * @return the initialized component instance
     */
    public Command getSend() {
        if (Send == null) {//GEN-END:|48-getter|0|48-preInit
            // write pre-init user code here
            Send = new Command("Send", Command.OK, 0);//GEN-LINE:|48-getter|1|48-postInit
            // write post-init user code here
        }//GEN-BEGIN:|48-getter|2|
        return Send;
    }
    //</editor-fold>//GEN-END:|48-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: serverNumber ">//GEN-BEGIN:|55-getter|0|55-preInit
    /**
     * Returns an initiliazed instance of serverNumber component.
     * @return the initialized component instance
     */
    public TextBox getServerNumber() {
        if (serverNumber == null) {//GEN-END:|55-getter|0|55-preInit
            // write pre-init user code here
            serverNumber = new TextBox("Server Number", null, 100, TextField.PHONENUMBER);//GEN-BEGIN:|55-getter|1|55-postInit
            serverNumber.addCommand(getSend());
            serverNumber.addCommand(getExitCommand());
            serverNumber.setCommandListener(this);//GEN-END:|55-getter|1|55-postInit
            // write post-init user code here
        }//GEN-BEGIN:|55-getter|2|
        return serverNumber;
    }
    //</editor-fold>//GEN-END:|55-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|80-getter|0|80-preInit
    /**
     * Returns an initiliazed instance of backCommand component.
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|80-getter|0|80-preInit
            // write pre-init user code here
            backCommand = new Command("Back", Command.BACK, 0);//GEN-LINE:|80-getter|1|80-postInit
            // write post-init user code here
        }//GEN-BEGIN:|80-getter|2|
        return backCommand;
    }
    //</editor-fold>//GEN-END:|80-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: saveCommand ">//GEN-BEGIN:|96-getter|0|96-preInit
    /**
     * Returns an initiliazed instance of saveCommand component.
     * @return the initialized component instance
     */
    public Command getSaveCommand() {
        if (saveCommand == null) {//GEN-END:|96-getter|0|96-preInit
            // write pre-init user code here
            saveCommand = new Command("Save", Command.OK, 0);//GEN-LINE:|96-getter|1|96-postInit
            // write post-init user code here
        }//GEN-BEGIN:|96-getter|2|
        return saveCommand;
    }
    //</editor-fold>//GEN-END:|96-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cancelCommand ">//GEN-BEGIN:|99-getter|0|99-preInit
    /**
     * Returns an initiliazed instance of cancelCommand component.
     * @return the initialized component instance
     */
    public Command getCancelCommand() {
        if (cancelCommand == null) {//GEN-END:|99-getter|0|99-preInit
            // write pre-init user code here
            cancelCommand = new Command("Cancel", Command.CANCEL, 1);//GEN-LINE:|99-getter|1|99-postInit
            // write post-init user code here
        }//GEN-BEGIN:|99-getter|2|
        return cancelCommand;
    }
    //</editor-fold>//GEN-END:|99-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: thesisInfoAlert ">//GEN-BEGIN:|105-getter|0|105-preInit
    /**
     * Returns an initiliazed instance of thesisInfoAlert component.
     * @return the initialized component instance
     */
    public Alert getThesisInfoAlert() {
        if (thesisInfoAlert == null) {//GEN-END:|105-getter|0|105-preInit
            // write pre-init user code here
            thesisInfoAlert = new Alert("Information", null, getInfoIcon(), AlertType.INFO);//GEN-BEGIN:|105-getter|1|105-postInit
            thesisInfoAlert.setTimeout(Alert.FOREVER);//GEN-END:|105-getter|1|105-postInit
            // write post-init user code here
        }//GEN-BEGIN:|105-getter|2|
        return thesisInfoAlert;
    }
    //</editor-fold>//GEN-END:|105-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: thesisErrorAlert ">//GEN-BEGIN:|108-getter|0|108-preInit
    /**
     * Returns an initiliazed instance of thesisErrorAlert component.
     * @return the initialized component instance
     */
    public Alert getThesisErrorAlert() {
        if (thesisErrorAlert == null) {//GEN-END:|108-getter|0|108-preInit
            // write pre-init user code here
            thesisErrorAlert = new Alert("alert", null, getErrorIcon(), null);//GEN-BEGIN:|108-getter|1|108-postInit
            thesisErrorAlert.setTimeout(Alert.FOREVER);//GEN-END:|108-getter|1|108-postInit
            // write post-init user code here
        }//GEN-BEGIN:|108-getter|2|
        return thesisErrorAlert;
    }
    //</editor-fold>//GEN-END:|108-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: serverSettingsForm ">//GEN-BEGIN:|115-getter|0|115-preInit
    /**
     * Returns an initiliazed instance of serverSettingsForm component.
     * @return the initialized component instance
     */
    public Form getServerSettingsForm() {
        if (serverSettingsForm == null) {//GEN-END:|115-getter|0|115-preInit
            // write pre-init user code here
            serverSettingsForm = new Form("Server Settings", new Item[] { getSaveRecFlagCG(), getFnPrfxTextField(), getPushRegCG(), getClientNumTF(), getNotificationsCG(), getStringItem(), getAutomatedGauge(), getAutomatedClientTF(), getAutomateVidDurCG() });//GEN-BEGIN:|115-getter|1|115-postInit
            serverSettingsForm.addCommand(getCancelCommand());
            serverSettingsForm.addCommand(getSaveCommand());
            serverSettingsForm.setCommandListener(this);//GEN-END:|115-getter|1|115-postInit
            // write post-init user code here
            serverSettingsForm.setItemStateListener(automatedGaugeStateListener);
        }//GEN-BEGIN:|115-getter|2|
        return serverSettingsForm;
    }
    //</editor-fold>//GEN-END:|115-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: notifcnsCommand ">//GEN-BEGIN:|113-getter|0|113-preInit
    /**
     * Returns an initiliazed instance of notifcnsCommand component.
     * @return the initialized component instance
     */
    public Command getNotifcnsCommand() {
        if (notifcnsCommand == null) {//GEN-END:|113-getter|0|113-preInit
            // write pre-init user code here
            notifcnsCommand = new Command("Notifications", Command.OK, 0);//GEN-LINE:|113-getter|1|113-postInit
            // write post-init user code here
        }//GEN-BEGIN:|113-getter|2|
        return notifcnsCommand;
    }
    //</editor-fold>//GEN-END:|113-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: fnPrfxTextField ">//GEN-BEGIN:|123-getter|0|123-preInit
    /**
     * Returns an initiliazed instance of fnPrfxTextField component.
     * @return the initialized component instance
     */
    public TextField getFnPrfxTextField() {
        if (fnPrfxTextField == null) {//GEN-END:|123-getter|0|123-preInit
            // write pre-init user code here
            fnPrfxTextField = new TextField("Filename Prefix", "thesis", 32, TextField.ANY);//GEN-LINE:|123-getter|1|123-postInit
            // write post-init user code here
        }//GEN-BEGIN:|123-getter|2|
        return fnPrfxTextField;
    }
    //</editor-fold>//GEN-END:|123-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: saveRecFlagCG ">//GEN-BEGIN:|124-getter|0|124-preInit
    /**
     * Returns an initiliazed instance of saveRecFlagCG component.
     * @return the initialized component instance
     */
    public ChoiceGroup getSaveRecFlagCG() {
        if (saveRecFlagCG == null) {//GEN-END:|124-getter|0|124-preInit
            // write pre-init user code here
            saveRecFlagCG = new ChoiceGroup("Recorded Files", Choice.MULTIPLE);//GEN-BEGIN:|124-getter|1|124-postInit
            saveRecFlagCG.append("Save Recorded Videos", null);
            saveRecFlagCG.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
            saveRecFlagCG.setSelectedFlags(new boolean[] { false });//GEN-END:|124-getter|1|124-postInit
            // write post-init user code here
        }//GEN-BEGIN:|124-getter|2|
        return saveRecFlagCG;
    }
    //</editor-fold>//GEN-END:|124-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: clientSettingsForm ">//GEN-BEGIN:|128-getter|0|128-preInit
    /**
     * Returns an initiliazed instance of clientSettingsForm component.
     * @return the initialized component instance
     */
    public Form getClientSettingsForm() {
        if (clientSettingsForm == null) {//GEN-END:|128-getter|0|128-preInit
            // write pre-init user code here
            clientSettingsForm = new Form("Client Settings", new Item[] { getDefSrvrTF(), getClntSettingsFlgCG() });//GEN-BEGIN:|128-getter|1|128-postInit
            clientSettingsForm.addCommand(getSaveCommand());
            clientSettingsForm.addCommand(getCancelCommand());
            clientSettingsForm.setCommandListener(this);//GEN-END:|128-getter|1|128-postInit
            // write post-init user code here
        }//GEN-BEGIN:|128-getter|2|
        return clientSettingsForm;
    }
    //</editor-fold>//GEN-END:|128-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: clntSettingsFlgCG ">//GEN-BEGIN:|130-getter|0|130-preInit
    /**
     * Returns an initiliazed instance of clntSettingsFlgCG component.
     * @return the initialized component instance
     */
    public ChoiceGroup getClntSettingsFlgCG() {
        if (clntSettingsFlgCG == null) {//GEN-END:|130-getter|0|130-preInit
            // write pre-init user code here
            clntSettingsFlgCG = new ChoiceGroup("Server", Choice.MULTIPLE);//GEN-BEGIN:|130-getter|1|130-postInit
            clntSettingsFlgCG.append("Automatically use server number", null);
            clntSettingsFlgCG.setSelectedFlags(new boolean[] { false });//GEN-END:|130-getter|1|130-postInit
            // write post-init user code here
        }//GEN-BEGIN:|130-getter|2|
        return clntSettingsFlgCG;
    }
    //</editor-fold>//GEN-END:|130-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: defSrvrTF ">//GEN-BEGIN:|132-getter|0|132-preInit
    /**
     * Returns an initiliazed instance of defSrvrTF component.
     * @return the initialized component instance
     */
    public TextField getDefSrvrTF() {
        if (defSrvrTF == null) {//GEN-END:|132-getter|0|132-preInit
            // write pre-init user code here
            defSrvrTF = new TextField("Default Server Number", null, 32, TextField.PHONENUMBER);//GEN-LINE:|132-getter|1|132-postInit
            // write post-init user code here
        }//GEN-BEGIN:|132-getter|2|
        return defSrvrTF;
    }
    //</editor-fold>//GEN-END:|132-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: pushRegCG ">//GEN-BEGIN:|137-getter|0|137-preInit
    /**
     * Returns an initiliazed instance of pushRegCG component.
     * @return the initialized component instance
     */
    public ChoiceGroup getPushRegCG() {
        if (pushRegCG == null) {//GEN-END:|137-getter|0|137-preInit
            // write pre-init user code here
            pushRegCG = new ChoiceGroup("Auto-startup settings", Choice.MULTIPLE);//GEN-BEGIN:|137-getter|1|137-postInit
            pushRegCG.append("Allow auto-startup", null);
            pushRegCG.setSelectedFlags(new boolean[] { false });//GEN-END:|137-getter|1|137-postInit
            // write post-init user code here
        }//GEN-BEGIN:|137-getter|2|
        return pushRegCG;
    }
    //</editor-fold>//GEN-END:|137-getter|2|
    //</editor-fold>
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: clientNumTF ">//GEN-BEGIN:|142-getter|0|142-preInit
    /**
     * Returns an initiliazed instance of clientNumTF component.
     * @return the initialized component instance
     */
    public TextField getClientNumTF() {
        if (clientNumTF == null) {//GEN-END:|142-getter|0|142-preInit
            // write pre-init user code here
            clientNumTF = new TextField("Auto start client number", null, 32, TextField.PHONENUMBER);//GEN-LINE:|142-getter|1|142-postInit
            // write post-init user code here
        }//GEN-BEGIN:|142-getter|2|
        return clientNumTF;
    }
    //</editor-fold>//GEN-END:|142-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: serverIcon ">//GEN-BEGIN:|143-getter|0|143-preInit
    /**
     * Returns an initiliazed instance of serverIcon component.
     * @return the initialized component instance
     */
    public Image getServerIcon() {
        if (serverIcon == null) {//GEN-END:|143-getter|0|143-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|143-getter|1|143-@java.io.IOException
                serverIcon = Image.createImage("/32x32/computer_21x21.png");
            } catch (java.io.IOException e) {//GEN-END:|143-getter|1|143-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|143-getter|2|143-postInit
            // write post-init user code here
        }//GEN-BEGIN:|143-getter|3|
        return serverIcon;
    }
    //</editor-fold>//GEN-END:|143-getter|3|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: clientIcon ">//GEN-BEGIN:|144-getter|0|144-preInit
    /**
     * Returns an initiliazed instance of clientIcon component.
     * @return the initialized component instance
     */
    public Image getClientIcon() {
        if (clientIcon == null) {//GEN-END:|144-getter|0|144-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|144-getter|1|144-@java.io.IOException
                clientIcon = Image.createImage("/32x32/home_21x21.png");
            } catch (java.io.IOException e) {//GEN-END:|144-getter|1|144-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|144-getter|2|144-postInit
            // write post-init user code here
        }//GEN-BEGIN:|144-getter|3|
        return clientIcon;
    }
    //</editor-fold>//GEN-END:|144-getter|3|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: notifcIcon ">//GEN-BEGIN:|145-getter|0|145-preInit
    /**
     * Returns an initiliazed instance of notifcIcon component.
     * @return the initialized component instance
     */
    public Image getNotifcIcon() {
        if (notifcIcon == null) {//GEN-END:|145-getter|0|145-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|145-getter|1|145-@java.io.IOException
                notifcIcon = Image.createImage("/32x32/direction_left_21x21.png");
            } catch (java.io.IOException e) {//GEN-END:|145-getter|1|145-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|145-getter|2|145-postInit
            // write post-init user code here
        }//GEN-BEGIN:|145-getter|3|
        return notifcIcon;
    }
    //</editor-fold>//GEN-END:|145-getter|3|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitIcon ">//GEN-BEGIN:|146-getter|0|146-preInit
    /**
     * Returns an initiliazed instance of exitIcon component.
     * @return the initialized component instance
     */
    public Image getExitIcon() {
        if (exitIcon == null) {//GEN-END:|146-getter|0|146-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|146-getter|1|146-@java.io.IOException
                exitIcon = Image.createImage("/32x32/user_delete_21x21.png");
            } catch (java.io.IOException e) {//GEN-END:|146-getter|1|146-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|146-getter|2|146-postInit
            // write post-init user code here
        }//GEN-BEGIN:|146-getter|3|
        return exitIcon;
    }
    //</editor-fold>//GEN-END:|146-getter|3|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: errorIcon ">//GEN-BEGIN:|147-getter|0|147-preInit
    /**
     * Returns an initiliazed instance of errorIcon component.
     * @return the initialized component instance
     */
    public Image getErrorIcon() {
        if (errorIcon == null) {//GEN-END:|147-getter|0|147-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|147-getter|1|147-@java.io.IOException
                errorIcon = Image.createImage("/32x32/notification_error_21x21.png");
            } catch (java.io.IOException e) {//GEN-END:|147-getter|1|147-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|147-getter|2|147-postInit
            // write post-init user code here
        }//GEN-BEGIN:|147-getter|3|
        return errorIcon;
    }
    //</editor-fold>//GEN-END:|147-getter|3|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: downldIcon ">//GEN-BEGIN:|148-getter|0|148-preInit
    /**
     * Returns an initiliazed instance of downldIcon component.
     * @return the initialized component instance
     */
    public Image getDownldIcon() {
        if (downldIcon == null) {//GEN-END:|148-getter|0|148-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|148-getter|1|148-@java.io.IOException
                downldIcon = Image.createImage("/32x32/clock_21x21.png");
            } catch (java.io.IOException e) {//GEN-END:|148-getter|1|148-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|148-getter|2|148-postInit
            // write post-init user code here
        }//GEN-BEGIN:|148-getter|3|
        return downldIcon;
    }
    //</editor-fold>//GEN-END:|148-getter|3|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: infoIcon ">//GEN-BEGIN:|149-getter|0|149-preInit
    /**
     * Returns an initiliazed instance of infoIcon component.
     * @return the initialized component instance
     */
    public Image getInfoIcon() {
        if (infoIcon == null) {//GEN-END:|149-getter|0|149-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|149-getter|1|149-@java.io.IOException
                infoIcon = Image.createImage("/32x32/message_21x21.png");
            } catch (java.io.IOException e) {//GEN-END:|149-getter|1|149-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|149-getter|2|149-postInit
            // write post-init user code here
        }//GEN-BEGIN:|149-getter|3|
        return infoIcon;
    }
    //</editor-fold>//GEN-END:|149-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|150-getter|0|150-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|150-getter|0|150-preInit
            // write pre-init user code here
            okCommand = new Command("Toggle Mode", Command.OK, 0);//GEN-LINE:|150-getter|1|150-postInit
            // write post-init user code here
        }//GEN-BEGIN:|150-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|150-getter|2|

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: splashImg ">//GEN-BEGIN:|159-getter|0|159-preInit
    /**
     * Returns an initiliazed instance of splashImg component.
     * @return the initialized component instance
     */
    public Image getSplashImg() {
        if (splashImg == null) {//GEN-END:|159-getter|0|159-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|159-getter|1|159-@java.io.IOException
                splashImg = Image.createImage("/disc.png");
            } catch (java.io.IOException e) {//GEN-END:|159-getter|1|159-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|159-getter|2|159-postInit
            // write post-init user code here
        }//GEN-BEGIN:|159-getter|3|
        return splashImg;
    }
    //</editor-fold>//GEN-END:|159-getter|3|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: settingsCommand ">//GEN-BEGIN:|160-getter|0|160-preInit
    /**
     * Returns an initiliazed instance of settingsCommand component.
     * @return the initialized component instance
     */
    public Command getSettingsCommand() {
        if (settingsCommand == null) {//GEN-END:|160-getter|0|160-preInit
            // write pre-init user code here
            settingsCommand = new Command("Settings", Command.OK, 0);//GEN-LINE:|160-getter|1|160-postInit
            // write post-init user code here
        }//GEN-BEGIN:|160-getter|2|
        return settingsCommand;
    }
    //</editor-fold>//GEN-END:|160-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: notificationsCG ">//GEN-BEGIN:|164-getter|0|164-preInit
    /**
     * Returns an initiliazed instance of notificationsCG component.
     * @return the initialized component instance
     */
    public ChoiceGroup getNotificationsCG() {
        if (notificationsCG == null) {//GEN-END:|164-getter|0|164-preInit
            // write pre-init user code here
            notificationsCG = new ChoiceGroup("Notifications", Choice.MULTIPLE);//GEN-BEGIN:|164-getter|1|164-postInit
            notificationsCG.append("Request received", null);
            notificationsCG.append("MMS sent", null);
            notificationsCG.append("Server errors", null);
            notificationsCG.setSelectedFlags(new boolean[] { false, false, false });//GEN-END:|164-getter|1|164-postInit
            // write post-init user code here
        }//GEN-BEGIN:|164-getter|2|
        return notificationsCG;
    }
    //</editor-fold>//GEN-END:|164-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">//GEN-BEGIN:|171-getter|0|171-preInit
    /**
     * Returns an initiliazed instance of stringItem component.
     * @return the initialized component instance
     */
    public StringItem getStringItem() {
        if (stringItem == null) {//GEN-END:|171-getter|0|171-preInit
            // write pre-init user code here
            stringItem = new StringItem("Automated Mode Settings", null);//GEN-LINE:|171-getter|1|171-postInit
            // write post-init user code here
        }//GEN-BEGIN:|171-getter|2|
        return stringItem;
    }
    //</editor-fold>//GEN-END:|171-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: automatedGauge ">//GEN-BEGIN:|172-getter|0|172-preInit
    /**
     * Returns an initiliazed instance of automatedGauge component.
     * @return the initialized component instance
     */
    public Gauge getAutomatedGauge() {
        if (automatedGauge == null) {//GEN-END:|172-getter|0|172-preInit
            // write pre-init user code here
            automatedGauge = new Gauge("Every 15 mins", true, 9, 3);//GEN-LINE:|172-getter|1|172-postInit
            // write post-init user code here
        }//GEN-BEGIN:|172-getter|2|
        return automatedGauge;
    }
    //</editor-fold>//GEN-END:|172-getter|2|
    private ItemStateListener automatedGaugeStateListener = new ItemStateListener() {

        public void itemStateChanged(Item item) {
            if (item == getAutomatedGauge()) {
                getAutomatedGauge().setLabel(
                        ThesisConstants.getTimeIntervalStr(getAutomatedGauge().getValue()));
            }
        }
    };
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: AutomatedClientTF ">//GEN-BEGIN:|173-getter|0|173-preInit
    /**
     * Returns an initiliazed instance of AutomatedClientTF component.
     * @return the initialized component instance
     */
    public TextField getAutomatedClientTF() {
        if (AutomatedClientTF == null) {//GEN-END:|173-getter|0|173-preInit
            // write pre-init user code here
            AutomatedClientTF = new TextField("Client", null, 32, TextField.PHONENUMBER);//GEN-LINE:|173-getter|1|173-postInit
            // write post-init user code here
        }//GEN-BEGIN:|173-getter|2|
        return AutomatedClientTF;
    }
    //</editor-fold>//GEN-END:|173-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: automateVidDurCG ">//GEN-BEGIN:|174-getter|0|174-preInit
    /**
     * Returns an initiliazed instance of automateVidDurCG component.
     * @return the initialized component instance
     */
    public ChoiceGroup getAutomateVidDurCG() {
        if (automateVidDurCG == null) {//GEN-END:|174-getter|0|174-preInit
            // write pre-init user code here
            automateVidDurCG = new ChoiceGroup("Video duration", Choice.EXCLUSIVE);//GEN-BEGIN:|174-getter|1|174-postInit
            automateVidDurCG.append("3 seconds", null);
            automateVidDurCG.append("5 seconds", null);
            automateVidDurCG.append("10 seconds", null);
            automateVidDurCG.setSelectedFlags(new boolean[] { true, false, false });//GEN-END:|174-getter|1|174-postInit
            // write post-init user code here
        }//GEN-BEGIN:|174-getter|2|
        return automateVidDurCG;
    }
    //</editor-fold>//GEN-END:|174-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand1 ">//GEN-BEGIN:|178-getter|0|178-preInit
    /**
     * Returns an initiliazed instance of okCommand1 component.
     * @return the initialized component instance
     */
    public Command getOkCommand1() {
        if (okCommand1 == null) {//GEN-END:|178-getter|0|178-preInit
            // write pre-init user code here
            okCommand1 = new Command("Settings", Command.OK, 0);//GEN-LINE:|178-getter|1|178-postInit
            // write post-init user code here
        }//GEN-BEGIN:|178-getter|2|
        return okCommand1;
    }
    //</editor-fold>//GEN-END:|178-getter|2|


    //</editor-fold>



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: task ">//GEN-BEGIN:|186-getter|0|186-preInit
    /**
     * Returns an initiliazed instance of task component.
     * @return the initialized component instance
     */
    public SimpleCancellableTask getTask() {
        if (task == null) {//GEN-END:|186-getter|0|186-preInit
            // write pre-init user code here
            task = new SimpleCancellableTask();//GEN-BEGIN:|186-getter|1|186-execute
            task.setExecutable(new org.netbeans.microedition.util.Executable() {
                public void execute() throws Exception {//GEN-END:|186-getter|1|186-execute
                    // write task-execution user code here
                }//GEN-BEGIN:|186-getter|2|186-postInit
            });//GEN-END:|186-getter|2|186-postInit
            // write post-init user code here
        }//GEN-BEGIN:|186-getter|3|
        return task;
    }
    //</editor-fold>//GEN-END:|186-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: aboutForm ">//GEN-BEGIN:|189-getter|0|189-preInit
    /**
     * Returns an initiliazed instance of aboutForm component.
     * @return the initialized component instance
     */
    public Form getAboutForm() {
        if (aboutForm == null) {//GEN-END:|189-getter|0|189-preInit
            // write pre-init user code here
            aboutForm = new Form("", new Item[] { getImageItem(), getStringItem1(), getStringItem2(), getStringItem3(), getStringItem4() });//GEN-BEGIN:|189-getter|1|189-postInit
            aboutForm.addCommand(getBackCommand());
            aboutForm.setCommandListener(this);//GEN-END:|189-getter|1|189-postInit
            // write post-init user code here
        }//GEN-BEGIN:|189-getter|2|
        return aboutForm;
    }
    //</editor-fold>//GEN-END:|189-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: imageItem ">//GEN-BEGIN:|196-getter|0|196-preInit
    /**
     * Returns an initiliazed instance of imageItem component.
     * @return the initialized component instance
     */
    public ImageItem getImageItem() {
        if (imageItem == null) {//GEN-END:|196-getter|0|196-preInit
            // write pre-init user code here
            imageItem = new ImageItem("", getOwlMed(), ImageItem.LAYOUT_CENTER, "<Missing Image>", Item.PLAIN);//GEN-LINE:|196-getter|1|196-postInit
            // write post-init user code here
        }//GEN-BEGIN:|196-getter|2|
        return imageItem;
    }
    //</editor-fold>//GEN-END:|196-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: owlBig ">//GEN-BEGIN:|195-getter|0|195-preInit
    /**
     * Returns an initiliazed instance of owlBig component.
     * @return the initialized component instance
     */
    public Image getOwlBig() {
        if (owlBig == null) {//GEN-END:|195-getter|0|195-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|195-getter|1|195-@java.io.IOException
                owlBig = Image.createImage("/owl240x320.jpg");
            } catch (java.io.IOException e) {//GEN-END:|195-getter|1|195-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|195-getter|2|195-postInit
            // write post-init user code here
        }//GEN-BEGIN:|195-getter|3|
        return owlBig;
    }
    //</editor-fold>//GEN-END:|195-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: owlMed ">//GEN-BEGIN:|197-getter|0|197-preInit
    /**
     * Returns an initiliazed instance of owlMed component.
     * @return the initialized component instance
     */
    public Image getOwlMed() {
        if (owlMed == null) {//GEN-END:|197-getter|0|197-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|197-getter|1|197-@java.io.IOException
                owlMed = Image.createImage("/owl120x160.jpg");
            } catch (java.io.IOException e) {//GEN-END:|197-getter|1|197-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|197-getter|2|197-postInit
            // write post-init user code here
        }//GEN-BEGIN:|197-getter|3|
        return owlMed;
    }
    //</editor-fold>//GEN-END:|197-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem1 ">//GEN-BEGIN:|198-getter|0|198-preInit
    /**
     * Returns an initiliazed instance of stringItem1 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem1() {
        if (stringItem1 == null) {//GEN-END:|198-getter|0|198-preInit
            // write pre-init user code here
            stringItem1 = new StringItem("About:", "Owl is a remote access surveilance mobile application. No Owls were harmed in the making of this software");//GEN-LINE:|198-getter|1|198-postInit
            // write post-init user code here
        }//GEN-BEGIN:|198-getter|2|
        return stringItem1;
    }
    //</editor-fold>//GEN-END:|198-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem2 ">//GEN-BEGIN:|199-getter|0|199-preInit
    /**
     * Returns an initiliazed instance of stringItem2 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem2() {
        if (stringItem2 == null) {//GEN-END:|199-getter|0|199-preInit
            // write pre-init user code here
            stringItem2 = new StringItem("Created and Owned By:", "De Rivera, Vicente III S.");//GEN-LINE:|199-getter|1|199-postInit
            // write post-init user code here
        }//GEN-BEGIN:|199-getter|2|
        return stringItem2;
    }
    //</editor-fold>//GEN-END:|199-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem3 ">//GEN-BEGIN:|200-getter|0|200-preInit
    /**
     * Returns an initiliazed instance of stringItem3 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem3() {
        if (stringItem3 == null) {//GEN-END:|200-getter|0|200-preInit
            // write pre-init user code here
            stringItem3 = new StringItem("", "Manzano, Erna Katrina");//GEN-LINE:|200-getter|1|200-postInit
            // write post-init user code here
        }//GEN-BEGIN:|200-getter|2|
        return stringItem3;
    }
    //</editor-fold>//GEN-END:|200-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem4 ">//GEN-BEGIN:|201-getter|0|201-preInit
    /**
     * Returns an initiliazed instance of stringItem4 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem4() {
        if (stringItem4 == null) {//GEN-END:|201-getter|0|201-preInit
            // write pre-init user code here
            stringItem4 = new StringItem("", "Glivano, Lisl Camille");//GEN-LINE:|201-getter|1|201-postInit
            // write post-init user code here
        }//GEN-BEGIN:|201-getter|2|
        return stringItem4;
    }
    //</editor-fold>//GEN-END:|201-getter|2|



    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        cleanConnections();
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        cleanConnections();
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

    public void showErrorAlert(String message) {
        getThesisErrorAlert().setString(message);
        switchDisplayable(getThesisErrorAlert(), Display.getDisplay(this).getCurrent());
    }

    public void showErrorAlert(Throwable th) {
        th.printStackTrace();
        showErrorAlert(th.getMessage());
    }

    public void showInfoAlert(String message) {
        getThesisInfoAlert().setString(message);
        switchDisplayable(getThesisInfoAlert(), Display.getDisplay(this).getCurrent());
    }

    private void cleanConnections() {
        if (thesisServer != null) {
            thesisServer.destroy();
            thesisServer = null;
        }
        if (thesisClient != null) {
            thesisClient.destroy();
            thesisClient = null;
        }
        System.gc();
    }

    private void saveSrvrSettings() throws Exception {
        // validation
        // Push reg Settigs
        boolean[] pushregflg = new boolean[1];
        getPushRegCG().getSelectedFlags(pushregflg);
        pushRegFlag = pushregflg[0];
        try {
            String cons[] = PushRegistry.listConnections(false);
            for (int i = 0; i < cons.length; i++) {
                PushRegistry.unregisterConnection(cons[i]);
            }
        } catch (Throwable t) {
            logServer(t);
        }
        if (pushregflg[0]) {
            if (clientNumTF.getString() == null
                    || clientNumTF.getString().equals(ThesisConstants.EMPTY)) {
                throw new Exception("Enter a client number");
            }
            clientNum = clientNumTF.getString();
            clientNum = validatePhoneNumber(clientNum);
            try {
                PushRegistry.registerConnection("sms://:" + ThesisConstants.SMSPORT, "app.ThesisMIDlet", clientNum);
            } catch (Throwable t) {
                logServer(t);
            }
        } else {
            clientNum = ThesisConstants.EMPTY;
        }
        thesisSrvrSettings.put("clntnum", clientNum);
        thesisSrvrSettings.put("pshregflg", pushRegFlag + ThesisConstants.EMPTY);

        // notifcations settingsFlag
        getNotificationsCG().getSelectedFlags(srvrSettingsFlag);
        for (int i = 0; i < srvrSettingsFlag.length; i++) {
            boolean b = srvrSettingsFlag[i];
            thesisSrvrSettings.put(i + ThesisConstants.EMPTY, b + ThesisConstants.EMPTY);
        }
        // filename prefix
        fileNamePrefix = getFnPrfxTextField().getString();
        fileNamePrefix.trim(); // TODO: error trap invalid filenames
        thesisSrvrSettings.put("fnprfx", fileNamePrefix);
        // save recorded file flag
        boolean[] svrcsflg = new boolean[1];
        getSaveRecFlagCG().getSelectedFlags(svrcsflg);
        saveRecsFlag = svrcsflg[0];
        thesisSrvrSettings.put("svrcsflg", saveRecsFlag + "");

        // automated settings
        automatedGaugeValue = getAutomatedGauge().getValue();
        automatedClientNum = getAutomatedClientTF().getString();
        automatedClientNum = validatePhoneNumber(automatedClientNum);
        automatedVidDurIdx = getAutomateVidDurCG().getSelectedIndex();
        thesisSrvrSettings.put("autogv", automatedGaugeValue + ThesisConstants.EMPTY);
        thesisSrvrSettings.put("autocn", automatedClientNum);
        thesisSrvrSettings.put("autovdidx", automatedVidDurIdx + ThesisConstants.EMPTY);


        try {
            thesisSrvrSettings.save();
        } catch (Throwable ex) {
            logServer(ex);
        }
    }

    private String validatePhoneNumber(String phoneNum) {
        if (phoneNum.startsWith("0")) {
            phoneNum = "+63" + phoneNum.substring(1);
        }
        return phoneNum;
    }

    private void loadSrvrSettings() {
        if (!thesisSrvrSettings.isInitial()) {
            for (int i = 0; i < srvrSettingsFlag.length; i++) {
                srvrSettingsFlag[i] = thesisSrvrSettings.get(ThesisConstants.EMPTY + i).equals(ThesisConstants.BOOLEAN_T) ? true : false;
            }
            getNotificationsCG().setSelectedFlags(srvrSettingsFlag);
            fileNamePrefix = thesisSrvrSettings.get("fnprfx");
            getFnPrfxTextField().setString(fileNamePrefix);
            saveRecsFlag = thesisSrvrSettings.get("svrcsflg").equals(ThesisConstants.BOOLEAN_T) ? true : false;
            getSaveRecFlagCG().setSelectedIndex(0, saveRecsFlag);
            pushRegFlag = thesisSrvrSettings.get("pshregflg").equals(ThesisConstants.BOOLEAN_T) ? true : false;
            getPushRegCG().setSelectedIndex(0, pushRegFlag);
            clientNum = thesisSrvrSettings.get("clntnum");
            getClientNumTF().setString(clientNum);
            automatedGaugeValue = Integer.parseInt(thesisSrvrSettings.get("autogv"));
            getAutomatedGauge().setValue(automatedGaugeValue);
            getAutomatedGauge().setLabel(ThesisConstants.getTimeIntervalStr(automatedGaugeValue));
            automatedClientNum = thesisSrvrSettings.get("autocn");
            getAutomatedClientTF().setString(automatedClientNum);
            automatedVidDurIdx = Integer.parseInt(thesisSrvrSettings.get("autovdidx"));
            getAutomateVidDurCG().setSelectedIndex(automatedVidDurIdx, true);
        }
    }

    private void loadClntSettings() {
        if (!thesisClntSettings.isInitial()) {
            for (int i = 0; i < clntSettingsFlag.length; i++) {
                clntSettingsFlag[i] = thesisClntSettings.get(ThesisConstants.EMPTY + i).equals(ThesisConstants.BOOLEAN_T) ? true : false;
            }
            getClntSettingsFlgCG().setSelectedFlags(clntSettingsFlag);
            defaultSrvrNum = thesisClntSettings.get("defsrvrnum");
            getDefSrvrTF().setString(defaultSrvrNum);
        }
    }

    private void saveClntSettings() {
        // settingsFlag
        getClntSettingsFlgCG().getSelectedFlags(clntSettingsFlag);
        for (int i = 0; i < clntSettingsFlag.length; i++) {
            boolean b = clntSettingsFlag[i];
            thesisClntSettings.put(i + ThesisConstants.EMPTY, b + ThesisConstants.EMPTY);
        }
        defaultSrvrNum = getDefSrvrTF().getString();
        thesisClntSettings.put("defsrvrnum", defaultSrvrNum);

        try {
            thesisClntSettings.save();
        } catch (Throwable ex) {
            showErrorAlert(ex);
        }
    }

    // SETTINGS GETTERS
    public boolean[] getClntSettingsFlag() {
        return clntSettingsFlag;
    }

    public String getDefaultSrvrNum() {
        return defaultSrvrNum;
    }

    public String getFileNamePrefix() {
        return fileNamePrefix;
    }

    public boolean isSaveRecsFlag() {
        return saveRecsFlag;
    }

    public boolean[] getSrvrSettingsFlag() {
        return srvrSettingsFlag;
    }

    public boolean isServerCapable() {
        try {
            Class.forName("javax.wireless.messaging.MultipartMessage");
        } catch (ClassNotFoundException ex) {
            return false;
        }
        return true;
    }

    public MessageConnection getSMSServer() {
        return SMSServer;
    }

    public boolean isStartedByReq() {
        return startedByReq;
    }

    public void setStartedByReq(boolean startedByReq) {
        this.startedByReq = startedByReq;
    }

    private void logServer(Throwable t) {
        logServer(t.getMessage());
    }

    private void logServer(String s) {
        StringItem si = new StringItem(null, s);
        si.setLayout(Item.LAYOUT_2 | Item.LAYOUT_LEFT | Item.LAYOUT_NEWLINE_AFTER);
        thesisServerLog.append(s);
    }

    private void initServer() {
        cleanConnections();
        if (isServerCapable()) {
            try {
                thesisServer = new ThesisServer(thesisServerLog, this);
            } catch (Throwable t) {
                logServer("ERROR: " + t.getMessage());
            }
        } else {
            logServer("Sorry, your mobile phone not capable as server");
        }
    }
    private Timer automatedTimer = null;
    private int autoSec = 0;

    private void toggleAutomatedServer() {
        if (automatedClientNum.equals(ThesisConstants.EMPTY)) {
            showErrorAlert(ThesisConstants.AUTOMATED_CLIENTNUM_ERROR);
            return;
        }
        if (automatedTimer == null) { // toggle start
            automatedTimer = new Timer();
            autoSec = 0;
            thesisServer.setAllowRequest(false);
        } else { // toggle stop
            automatedTimer.cancel();
            automatedTimer = null;
            getThesisServerLog().setTitle("Server Log");
            thesisServer.setMmsSubject("Thesis MMS");
            thesisServer.setAllowRequest(true);
            return;
        }
        thesisServer.setMmsSubject("Thesis MMS - Auto Mode");
        automatedTimer.schedule(new TimerTask() {

            int min;
            int duration;

            public void run() {
                autoSec++;
                min = autoSec / 60;
                duration = ThesisConstants.getChoiceDurationFromCGIdx(automatedVidDurIdx);
                getThesisServerLog().setTitle("Auto: "
                        + (min < 10 ? "0" : "") + min
                        + ":" + (autoSec % 60 < 10 ? "0" : "") + autoSec % 60);
                if (autoSec % ThesisConstants.getTimeIntervalSec(automatedGaugeValue) == 0) {
                    thesisServer.setClientNum(automatedClientNum);
                    getThesisServerLog().setTitle("Recording for " + duration + " seconds");
                    thesisServer.initPlayer();
                    thesisServer.doRecord(duration);
                }

            }
        }, 0, 1000L);
    }
}
